package ru.spies.content;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import ru.spies.forms.LoginOrRegistrationForm;
import ru.spies.forms.SystemForm;
import ru.spies.help.MyLogger;

public class ChooseContent {
	
	private static ChooseContent statThis=null;
	private static EIndexContentType curContent=null;
	private static String curText=null;
	
	private static String INDEX_FILE="C:/xampp/tomcat/kyrsach/indexContent.txt";
	private static String NEWS_FILE="C:/xampp/tomcat/kyrsach/newsContent.txt";
	//private static String SYSTEM_FILE="C:/xampp/tomcat/kyrsach/systemContent.txt";
	//private static String LOGIN_FILE="C:/xampp/tomcat/kyrsach/loginContent.txt";
	private static String ABOUT_FILE="C:/xampp/tomcat/kyrsach/aboutContent.txt";
	private static String LAST_UPDATES_FILE="C:/xampp/tomcat/kyrsach/lastUpdatesContent.txt";
	private static String SITE_MAP_FILE="C:/xampp/tomcat/kyrsach/siteMapContent.txt";
	
	private static String COOKIE_NAME ="content";
	
	private static String COOKIE_INDEX="INDEX";
	private static String COOKIE_NEWS="NEWS";
	private static String COOKIE_SYSTEM="SYSTEM";
	private static String COOKIE_LOGIN="LOGIN";
	private static String COOKIE_ABOUT="ABOUT";
	private static String COOKIE_LAST_UPDATES="LAST_UPDATES";
	private static String COOKIE_SITE_MAP="SITE_MAP";
	
	
	private ChooseContent() {
		curContent=EIndexContentType.INDEX;
		chooseIndexContent();
	}
	
	public static ChooseContent getInstance(){
		if (statThis==null){
			statThis=new ChooseContent();
		}
		return statThis;
	}
	
	private String getTextFromFile(String filename){
		String content=new String();
		File f = new File(filename);
		Scanner in;
		try {
			in = new Scanner(f);
			while (in.hasNextLine()){
				content+=in.nextLine();
			}
			in.close();
		} catch (FileNotFoundException e) {
			MyLogger.getInstance().getLog().error(MyLogger.EXCEPTION,e);
		}
		return content;
	}
	
	public EIndexContentType getCurrentContent() {
		return curContent;
	}

	public void setCurrentContent(EIndexContentType curContent) {
		ChooseContent.curContent = curContent;
	}
	

	public void chooseIndexContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonHomeClass(CurrentButtonClass.ACTIVE_MENU_BUTTON);
		curText=getTextFromFile(INDEX_FILE);
	}
	
	public void chooseNewsContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonNewsClass(CurrentButtonClass.ACTIVE_MENU_BUTTON);
		curText=getTextFromFile(NEWS_FILE);
	}
	
	public void chooseSystemContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonSystemClass(CurrentButtonClass.ACTIVE_MENU_BUTTON);
		curText=SystemForm.getInstance().createThisForm();
	}
	
	public void chooseLoginContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonLoginClass(CurrentButtonClass.ACTIVE_MENU_BUTTON);
		curText=LoginOrRegistrationForm.getInstance().createThisForm();
	}
	
	public void chooseAboutContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonAboutClass(CurrentButtonClass.LINK_PANEL_ACTIVE);
		curText=getTextFromFile(ABOUT_FILE);
	}
	
	public void chooseLastUpdatesContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonLastUpdatesClass(CurrentButtonClass.LINK_PANEL_ACTIVE);
		curText=getTextFromFile(LAST_UPDATES_FILE);
	}
	
	public void chooseSiteMapContent(){
		resetBtn();
		CurrentButtonClass.getInstance().setButtonSiteMapClass(CurrentButtonClass.LINK_PANEL_ACTIVE);
		curText=getTextFromFile(SITE_MAP_FILE);
	}
	
	public void chooseDefault(){
		curText="<p>You have some errors!!!</p>";
	}
	
	public void readCookieAndSetNewChoose(HttpServletRequest req){
		Cookie cook[]=req.getCookies();
		if (cook==null){
			return;
		}
		for(Cookie i:cook){
			if (i.getName().equals(COOKIE_NAME)){
				if (i.getValue().equals(COOKIE_INDEX)){
					setCurrentContent(EIndexContentType.INDEX);
				} else {
					if (i.getValue().equals(COOKIE_NEWS)){
						setCurrentContent(EIndexContentType.NEWS);
					} else {
						if (i.getValue().equals(COOKIE_ABOUT)){
							setCurrentContent(EIndexContentType.ABOUT);
						} else {
							if (i.getValue().equals(COOKIE_LAST_UPDATES)){
								setCurrentContent(EIndexContentType.LAST_UPDATES);
							} else {
								if (i.getValue().equals(COOKIE_LOGIN)){
									setCurrentContent(EIndexContentType.LOGIN);
								} else {
									if (i.getValue().equals(COOKIE_SITE_MAP)){
										setCurrentContent(EIndexContentType.SITE_MAP);
									} else {
										if (i.getValue().equals(COOKIE_SYSTEM)){
											setCurrentContent(EIndexContentType.SYSTEM);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public String getCurrentText() {
		return curText;
	}
	
	private void resetBtn(){
		CurrentButtonClass.getInstance().resetAllButtons();
	}
	
}
